package server.handler;

import message.GroupMembersRequestMessage;
import message.GroupMembersResponseMessage;
import server.common.R;
import server.service.ChatGroupService;
import server.service.factories.ChatGroupServiceFactory;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;

import java.util.Set;

@ChannelHandler.Sharable
public class GroupMembersRequestMessageHandler extends SimpleChannelInboundHandler<GroupMembersRequestMessage> {
    @Override
    protected void channelRead0(ChannelHandlerContext ctx, GroupMembersRequestMessage msg) throws Exception {
        String groupName = msg.getGroupName();
        ChatGroupService chatGroupService= ChatGroupServiceFactory.getChatGroupService();
        R<Set<String>> result = chatGroupService.getMembers(groupName);
        if(result.getCode()!=1){
            ctx.channel().writeAndFlush(new GroupMembersResponseMessage(false,result.getMsg()));
        }
        else {
            Set<String> members = result.getData();
            ctx.channel().writeAndFlush(new GroupMembersResponseMessage(members));
        }

    }
}
